
layui.use(['layer', 'element','form'], function () { //独立版的layer无需执行这一句
  var $ = layui.jquery, layer = layui.layer, element = layui.element, form = layui.form; //独立版的layer无需执行这一句

  var loginUrls = jsRoutes.controllers.UsersController.login();
  var upwdUrls = jsRoutes.controllers.UsersController.pwdUpdated();
  var dingTalkAuth = jsRoutes.controllers.UsersController.dingTalkAuth();
  var authUser = jsRoutes.controllers.UsersController.authUser();
  var indexPC = jsRoutes.controllers.UsersController.index();
   var nasUserUpdate = jsRoutes.controllers.UserNasController.userUpdate();

  if ( document.getElementsByTagName("body")[0].accessKey == "nologin" && dd.env.platform !== "notInDingTalk") {
    dd.ready(function () {
    dd.runtime.permission.requestAuthCode({
      corpId: dingCorpId, // 企业id
      onSuccess: function (info) {
        code = info.code // 通过该免登授权码可以获取用户身份
        $.get({
          url: dingTalkAuth.url,
          data: { "code": code,"company":company },
          dataType: 'text',
          success: function (data,status) {
//      console.log(data);
//      alert("数据: \n" + data + "\n状态: " + status);
//       document.getElementById('username').innerHTML = '<' + data.uname + '>';
//      alert(data.getAllResponseHeaders());
      //sessionStorage.setItem("session", "Smith");
            window.top.location.href = indexPC.url;
          }
        });
      }
    });
  });
  }

  //触发事件
  $('#layerOpen .lay-btn').on('click', function () {
    var othis = $(this), method = othis.data('method');
    active[method] ? active[method].call(this, othis) : '';
  });

   form.on('submit(formDemo)', function(data){
   //layer.msg(JSON.stringify(data.field));
   console.log(data.field.csrfToken)
   $.post({  url: authUser.url,
     data: JSON.stringify(data.field),
     dataType: 'json',
     contentType:"application/json",
     headers: {"Csrf-Token": $('[name="csrfToken"]').val()},
     success: function (data,status,xhr) {
           console.log(data);
           var Token = xhr.getResponseHeader("Token")
           console.log("Token= "+ Token)
//           addCookie("token", Token)
           layer.msg(data.message);
           document.getElementById('login-info').innerHTML = data.message;
            if(data.url){window.top.location.href = data.url;}

         }
       });
   return false;
  });


     $('#cpacha-img').on('click', function () {
    $("#cpacha-img").attr("src",'/users/cpacha?vl=4&w=150&h=40&type=loginCpacha&t=' + new Date().getTime());
    })


//  jigsaw.init(document.getElementById('captcha'), function () {
//    $.post({  url: authUser.url,
//       data: JSON.stringify({p1:$('[name="p1"]').val(),p2:$('[name="p2"]').val()}),
//       dataType: 'json',
//       contentType:"application/json",
//       headers: {"Csrf-Token": $('[name="csrfToken"]').val()},
//       success: function (data,status,xhr) {
//          document.getElementById('msg').innerHTML = data.success;
//          if (data.success=='user.wrong') ;
//          if(data.url){window.top.location.href = data.url;}
//       }
//    });
//
//  })

});
